package internal

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/google/uuid"
	"math/rand"
	"net/url"
	"time"
	"vip_video/configx"
)

// NowTimestamp 获取显示在时间戳
func NowTimestamp() int64 {
	return time.Now().Unix()
}

// RandStr 生成随机字符串
func RandStr(length int) string {
	const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	r := rand.New(rand.NewSource(time.Now().Unix()))
	b := make([]byte, length)
	for i := range b {
		b[i] = charset[r.Intn(len(charset))]
	}
	return string(b)
}

// MaskStr 字符串打码
func MaskStr(originStr string) string {
	return originStr[:1] + "****" + originStr[len(originStr)-1:]
}

// Md5Password 密码加密
func Md5Password(str string, salt string) string {
	m := md5.New()
	m.Write([]byte(str))
	step1 := hex.EncodeToString(m.Sum(nil))
	step2M := md5.New()
	step2M.Write([]byte(step1 + salt))
	return hex.EncodeToString(step2M.Sum(nil))
}

// Md5Str md5加密字符串
func Md5Str(str string) string {
	m5 := md5.New()
	m5.Write([]byte(str))
	return hex.EncodeToString(m5.Sum(nil))
}

// MergeMaps 合并多个 map
func MergeMaps(maps ...map[string]any) map[string]any {
	result := make(map[string]any)

	for _, m := range maps {
		for key, value := range m {
			// 如果键已经存在，可以选择覆盖或者进行其他处理
			result[key] = value
		}
	}

	return result
}

// GenerateUUID 生成 uuid
func GenerateUUID() string {
	uuid := uuid.New()
	return uuid.String()
}

// ParseStaticUrl 解析静态资源地址
func ParseStaticUrl(path string) string {
	u := &url.URL{
		Scheme: configx.C.Static.Scheme,
		Host:   configx.C.Static.Host,
		Path:   path,
	}
	return u.String()
}
